package com.ledim.adapter;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.ChatRoomBean;
import com.ledim.datastatistics.LeDimAnalysisUtils;
import com.ledim.ledimview.LedimUserIconView;
import com.ledim.utils.DensityUtil;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.LedimFunctionUtils;
import com.ledim.utils.LedimSpUtils;
import com.letv.android.client.LetvSDK;
import com.letv.android.young.client.R;
import com.letv.core.bean.IVideo;
import com.letv.tracker2.enums.EventType;
import com.letv.tracker2.enums.Key;
import java.util.HashMap;
import java.util.Map;

import static com.letv.android.young.client.R.id.fl_room;

/**
 * Created by ccx on 16/6/20.
 * 首页放映厅adapter
 */
public class LedimRoomListAdapter extends LedimBaseListAdapter<ChatRoomBean> {

    public LedimRoomListAdapter(Context context) {
        super(context);
    }

    public Map<String, String> cardIds = new HashMap<String, String>();

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.item_ledim_room, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == 0) {
            holder.space.setVisibility(View.GONE);
        } else {
            holder.space.setVisibility(View.VISIBLE);
        }
        final ChatRoomBean room = getItem(position);

        if (room.cover != null && room.cover.thumb != null && room.cover.thumb.length() > 0) {
            FrescoUtil.loadUrl(room.cover.thumb, holder.iv_room_pic);
        }
        if (room.user != null) {
            if (!TextUtils.isEmpty(room.user.coin)) {
                holder.tv_coin_counts.setText(room.user.coin);
            }

            if (room.user.avatar != null && !TextUtils.isEmpty(room.user.avatar.thumb)) {
                holder.iv_room_creater.setUserIcon(room.user.userId, room.user.status, room.user.avatar.thumb, false);
            } else {
                holder.iv_room_creater.setUserIcon(room.user.userId, room.user.status, "", false);
            }

            holder.room_creater_name.setText(room.user.nickname);
        }
        if (room.name != null && room.name.length() > 0) {
            holder.tv_room_name.setText(room.name);
        } else {
            holder.tv_room_name.setText("放映厅名称");
        }

        GradientDrawable myGrad = (GradientDrawable) holder.ll_play_state.getBackground();
        if (room.status == 1) {
            myGrad.setColor(LedimFunctionUtils.getRandomColor(context));
            holder.iv_playing_state.setImageResource(R.drawable.room_playing_icon);
            if (room.current != null && room.current.length() > 0 && !room.current.equals("null")) {
                holder.tv_room_videoname.setText(room.current.trim());

            } else {
                holder.tv_room_videoname.setText("洗脑循环中");
            }
        } else {
            holder.tv_room_videoname.setText("放映结束");
            myGrad.setColor(context.getResources().getColor(R.color.color_white_72percent));
            holder.iv_playing_state.setImageResource(R.drawable.room_stop_icon);
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IVideo roomVideo = new IVideo();
                roomVideo.setmVid(0);
                HashMap<String, String> params = new HashMap<String, String>();
                params.put("roomId", room.roomId);
                LeDimAnalysisUtils.reportEvent(LeDimAnalysisUtils.ROOM_PAGE_CODE, EventType.Click, params);
                if (room.user.userId.equals(LedimSpUtils.getUid())) {
                    LetvSDK.getInstance().playRoom(context, roomVideo, room, true, false);
                } else {
                    LetvSDK.getInstance().playRoom(context, roomVideo, room);
                }

            }
        });

        cardIds.put(room.roomId, "1");
        return convertView;
    }

    public static class ViewHolder {

        private SimpleDraweeView iv_room_pic;
        private LedimUserIconView iv_room_creater;
        private TextView tv_room_name, room_creater_name, tv_room_videoname, tv_coin_counts;
        private View ll_play_state, space;
        private ImageView iv_playing_state;


        public ViewHolder(View convertView) {
            iv_room_creater = (LedimUserIconView) convertView.findViewById(R.id.iv_room_creater);
            iv_room_pic = (SimpleDraweeView) convertView.findViewById(R.id.iv_room_pic);
            tv_room_name = (TextView) convertView.findViewById(R.id.tv_room_name);
            tv_room_videoname = (TextView) convertView.findViewById(R.id.tv_room_videoname);
            room_creater_name = (TextView) convertView.findViewById(R.id.room_creater_name);
            tv_coin_counts = (TextView) convertView.findViewById(R.id.tv_coin_counts);
            ll_play_state = convertView.findViewById(R.id.ll_play_state);
            iv_playing_state = (ImageView) convertView.findViewById(R.id.iv_playing_state);
            space = convertView.findViewById(R.id.space);
        }
    }

    public void postRoomExposeEvent() {
        if (!cardIds.isEmpty()) {
            for (Map.Entry<String, String> entry : cardIds.entrySet()) {
                HashMap<String, String> params = new HashMap<String, String>();
                params.put(Key.Type.name(), "roomId");
                params.put("roomId", entry.getKey());
                LeDimAnalysisUtils.reportEvent(LeDimAnalysisUtils.ROOM_PAGE_CODE, EventType.Expose, params);
            }
            cardIds.clear();
        }

    }

}
